package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Post;

@Repository
public class PostDaoImpl {
	@Autowired
	SessionFactory sessionFactory;
	
	public void addPost(Post post) {
		try{
			sessionFactory.getCurrentSession().save(post);
			}catch(Exception ex){
				ex.printStackTrace();
			}
	}
	
	@SuppressWarnings("unchecked")
	public List<Post> getAllPostsByEmpId(Employee emp) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from Post p where p.poster.id = :empId or p.poster.company.id = :companyId order by post_date desc");
		 query.setMaxResults(5);
		 query.setParameter("empId", emp.getId());
		 query.setParameter("companyId", emp.getCompany().getId());
		 List<Post> posts = new ArrayList<Post>(); 
		 posts = query.list();
		 return posts;
		  
		  
	}
	public Post getPostById(Integer id) {
		Post post = (Post)sessionFactory.getCurrentSession().get(Post.class, id);
		return post;		
	}
	@SuppressWarnings("unchecked")
	public List<Post> getPostsByMe(Employee employee) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from Post p where p.poster.id = :empId" );
		 query.setParameter("empId", employee.getId());
		 List<Post> posts = new ArrayList<Post>(); 
		 posts = query.list();
		 return posts;
		
	}

	@SuppressWarnings("unchecked")
	public List<Post> getPostsByCompany(Employee employee) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from Post p where  p.poster.company.id = :companyId");
		 System.out.println("((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))"+employee.getCompany().getId());
		 query.setParameter("companyId", employee.getCompany().getId());
		 List<Post> posts = new ArrayList<Post>(); 
		 posts = query.list();
		 return posts;
		
	}
	public List<Post> getPostsFromFollow(Employee employee) {
		return null;
		
	}
	public List<Post> getTopFivePostByDate(Employee employee) {
		  Session session = sessionFactory.getCurrentSession();
		 // Query query  = session.createQuery("from Post p where p.poster")
		  
		return null;
		
	}
}
